MySQL 安装与启停 二进制安装

1 背景知识

本文主要介绍使用 yum 方式安装 MySQL 数据库。

2 二进制部署

Warning

使用二进制安装这种模式不同于yum 安装自动分析相关依赖包。必须首先移除mariadb或者以前的安装过mysql 的配置文件。例如/etc/my.conf.

2.1 OS 环境准备

2.1.1 卸载mariadb

shell> yum remove mariadb*

2.1.2 安装依赖包

shell> yum search libaio  # search for info
shell> yum install libaio # install library

2.1.3 创建MySQL用户和组

shell> groupadd mysql
shell> useradd -r -g mysql -s /bin/false mysql

2.1.4 解压软件

shell> cd /usr/local
shell> tar zxvf /path/to/mysql-VERSION-OS.tar.gz

shell> gunzip < /path/to/mysql-VERSION-OS.tar.gz | tar xvf -

2.1.5 创建新的链接文件到真实目录

shell> ln -s full-path-to-mysql-VERSION-OS mysql

2.1.6 设置环境变量

shell> export PATH=$PATH:/usr/local/mysql/bin

2.1.7 配置启动目录

shell> cd mysql
shell> mkdir mysql-files
shell> chown mysql:mysql mysql-files
shell> chmod 750 mysql-files

2.2 初始化数据库

2.2.1 默认初始化方式

shell> bin/mysqld --initialize --user=mysql

2.2.2 初始化数据库两种方式

shell> bin/mysqld --initialize --user=mysql
shell> bin/mysqld --initialize-insecure --user=mysql
Warning

两种初始化方式第一种必须强制使用密码登录。第二种方式可以使用跳过密码
shell> mysql -u root --skip-password
然后更改对应密码
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'new_password';
在mysql 8.0 默认加密的密码已经改了。现在默认使用caching_sha2_password 加密方式。
如果还需使用默认的mysql_native_password 加密方式的话使用默认模式。
初始化的时候,默认路径为/usr/local/mysql

2.2.3 使用命令行指定数据库集簇目录

shell> bin/mysqld --initialize --user=mysql
         --basedir=/opt/mysql/mysql
         --datadir=/opt/mysql/mysql/data
[mysqld]
basedir=/opt/mysql/mysql
datadir=/opt/mysql/mysql/data

2.2.4 使用配置文件指定数据库集簇目录

  1. 命令
shell> bin/mysqld --initialize-insecure --user=mysql \
--basedir=/app/mysql \
         --datadir=/app/mysql/mysql-files
  1. 输出日志:
[root@mysql1 mysql]# bin/mysqld --initialize-insecure --user=mysql \
> --basedir=/app/mysql \
>          --datadir=/app/mysql/mysql-files
2018-09-28T16:18:32.178288Z 0 [System] [MY-013169] [Server] /app/mysql-commercial-8.0.12-el7-x86_64/bin/mysqld (mysqld 8.0.12-commercial) initializing of server in progress as process 5328
2018-09-28T16:18:46.268910Z 5 [Warning] [MY-010453] [Server] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.
2018-09-28T16:18:57.082553Z 0 [System] [MY-013170] [Server] /app/mysql-commercial-8.0.12-el7-x86_64/bin/mysqld (mysqld 8.0.12-commercial) initializing of server has completed

2.3 创建SSL 文件

2.3.1 说明

Format Description
[--datadir](file:///D:/refman-8.0-en.html-chapter/programs.html#option_mysql_ssl_rsa_setup_datadir) Path to data directory
[--help](file:///D:/refman-8.0-en.html-chapter/programs.html#option_mysql_ssl_rsa_setup_help) Display help message and exit
[--suffix](file:///D:/refman-8.0-en.html-chapter/programs.html#option_mysql_ssl_rsa_setup_suffix) Suffix for X509 certificate Common Name attribute
[--uid](file:///D:/refman-8.0-en.html-chapter/programs.html#option_mysql_ssl_rsa_setup_uid) Name of effective user to use for file permissions
[--verbose](file:///D:/refman-8.0-en.html-chapter/programs.html#option_mysql_ssl_rsa_setup_verbose) Verbose mode
[--version](file:///D:/refman-8.0-en.html-chapter/programs.html#option_mysql_ssl_rsa_setup_version) Display version information and exit

2.3.2 SSL 密钥和证书的默认目录

shell> bin/mysql_ssl_rsa_setup

2.3.3 SSL 密钥和证书的指定目录

bin/mysql_ssl_rsa_setup --datadir=/app/mysql/mysql-files/

3 启动和关闭

 shell> bin/mysqld_safe --user=mysql &
# 6. Next command is optional

3.1.1 拷贝自启动文件

shell> cp /app/mysql/support-files/mysql.server /etc/init.d/

3.1.2 编辑my.cnf 文件

basedir=/app/mysql
datadir=/app/mysql/mysql-files

3.1.3 重载启动项目

systemctl  daemon-reload

3.1.4 查看是否启动MySQL

systemctl  status mysql

3.1.5 开机自启动

Warning

由于使用二进制安装,则需要配置chkconfig 。RPM 包不需要配置此选项。

chkconfig --add mysql.server
chkconfig mysql.server on

4 账户安全

mysql -u root -p

ALTER USER 'root'@'localhost' IDENTIFIED BY 'new_password';
mysql -u root --skip-password
ALTER USER 'root'@'localhost' IDENTIFIED BY '123'